/**************************************************************************
 * @file usr_display_handle.c
 * @author 黑马小乌龟 (532352406@qq.com)
 * @brief 
 * @version 0.1
 * @date 2025-10-21
 * 
 * @copyright Copyright (c) 2025
 * 
 *************************************************************************/
#include "usr_pd_i2c.h"
#include "usr_display_handle.h"



RAM_FUNC int usr_get_dis_7_num_str(char *p_dis_buf,char *unit,int value){
    char tmp_buf[10]={0};
    int integer_str,fraction_str;

    if(p_dis_buf == NULL || unit == NULL)return -1;

    integer_str = value/1000u;
    fraction_str = value%1000u;

    if(integer_str<10){
        if(fraction_str<10){
            sprintf(tmp_buf,"00%d",fraction_str);
        }else if(fraction_str<100){
            sprintf(tmp_buf,"0%d",fraction_str);
        }else{
            sprintf(tmp_buf,"%d",fraction_str);
        }
    }else if(integer_str<100){
        fraction_str/=10;
       if(fraction_str<10){
            sprintf(tmp_buf,"0%d",fraction_str);
        }else if(fraction_str<100){
            sprintf(tmp_buf,"%d",fraction_str);
        }else{
            sprintf(tmp_buf,"%d",(fraction_str/10));
        }     
    }else if(integer_str<1000){
        sprintf(tmp_buf,"%d",(fraction_str/100));
    }else{
        sprintf(p_dis_buf,"%d%s",integer_str,unit);
        return 0;
    }

    sprintf(p_dis_buf,"%d.%s%s",integer_str,tmp_buf,unit);
    return 0;
    
}
#if LCD_PANEL_ENABLE 
void usr_cfg_item_display(CfgMenuActiveT cfg_state,int32_t cfg_vo,int32_t cfg_io) {
    char str_buf[64]={0};
    
    const ASCII_FONT_INFOR_T *vo_font,*io_font;

    switch (cfg_state) {
        case CFG_VOLTAGE_ACTIVE:
            vo_font = &ascii_16x16_font_rev;
            io_font = &ascii_16x16_font;
            main_dat.cfg_active_timeout = 6000u;
            usr_set_output_vol(&main_dat.buck_boost, cfg_vo);
            
            break;
        case CFG_CURRENT_ACTIVE:
            vo_font = &ascii_16x16_font;
            io_font = &ascii_16x16_font_rev;
            main_dat.cfg_active_timeout = 6000u;
            usr_set_output_current(&main_dat.buck_boost, cfg_io);
            break;
        case CFG_MENU_NULL:
        default:
            vo_font = &ascii_16x16_font;
            io_font = &ascii_16x16_font;
            main_dat.cfg_active_timeout = 0;
            break;
    }

    // sprintf(str_buf,"%d.%dV",cfg_vo/1000u,cfg_vo%1000u);
    usr_get_dis_7_num_str(str_buf,"V",cfg_vo);
    lcd_ascii_str_dis(4, 0u, *vo_font, str_buf);
    DDL_Printf("vo:%s\r\n", str_buf);
    
    // sprintf(str_buf,"%d.%dA",cfg_io/1000u,cfg_io%1000u);
    usr_get_dis_7_num_str(str_buf,"A",cfg_io);
    lcd_ascii_str_dis(6, 0u, *io_font, str_buf);
    DDL_Printf("io:%s\r\n", str_buf);
}
#else
void usr_cfg_item_display(CfgMenuActiveT cfg_state, int32_t cfg_vo, int32_t cfg_io) {
    char str_buf[64] = {0};

    // const ASCII_FONT_INFOR_T *vo_font, *io_font;

    switch (cfg_state) {
        case CFG_VOLTAGE_ACTIVE:
            // vo_font = &ascii_16x16_font_rev;
            // io_font = &ascii_16x16_font;
            main_dat.cfg_active_timeout = 6000u;
            usr_set_output_vol(&main_dat.buck_boost, cfg_vo);
            usr_get_dis_7_num_str(str_buf, "V", cfg_vo);
            usr_uart_send_a_msg(SET_OUT_VOLTAGE,str_buf);
            break;
        case CFG_CURRENT_ACTIVE:
            // vo_font = &ascii_16x16_font;
            // io_font = &ascii_16x16_font_rev;
            main_dat.cfg_active_timeout = 6000u;
            usr_set_output_current(&main_dat.buck_boost, cfg_io);
            usr_get_dis_7_num_str(str_buf, "A", cfg_io);
            usr_uart_send_a_msg(SET_OUT_CURRENT,str_buf);
            break;

        case CFG_MENU_NULL:
        default:
            // vo_font = &ascii_16x16_font;
            // io_font = &ascii_16x16_font;
            main_dat.cfg_active_timeout = 0;
            break;
    }
}
#endif

void usr_sync_cfg_to_host_handle(void){
    char vc_str[10] = {0};
    char ic_str[10] = {0};
    char send_buf[64]={0};

    usr_get_dis_7_num_str(vc_str, "V", main_dat.usr_cfg.cfg_vo);
    usr_get_dis_7_num_str(ic_str, "A", main_dat.usr_cfg.cfg_io);

    sprintf(send_buf,"<%s=MCU:%s><%s=PD:V%d.%d><%s=%s><%s=%s>",\
        BUCK_BOOST_MCU_VER,FW_VERSION,\
        PD_MCU_VERSION,(main_dat.pd_ver>>4),(main_dat.pd_ver&0x0f),\
        SET_OUT_VOLTAGE, vc_str,\
        SET_OUT_CURRENT, ic_str
    );

    DDL_Printf("%s\n",send_buf);

    usr_uart_send(send_buf,strlen(send_buf));
    usb_pd_information_handle(1);
}

void usr_cfg_item_active_handle(CfgMenuActiveT cfg_state) {
    // char str_buf[64]={0};
    
    // const ASCII_FONT_INFOR_T *vo_font,*io_font;

    switch (cfg_state) {
        case CFG_VOLTAGE_ACTIVE:
            main_dat.cfg_active_timeout = 6000u;
            usr_uart_send_a_msg(SET_ACTIVE_INFOR,"va");
            break;
        case CFG_CURRENT_ACTIVE:
            main_dat.cfg_active_timeout = 6000u;
            usr_uart_send_a_msg(SET_ACTIVE_INFOR,"ia");
            break;
        case CFG_MENU_NULL:
        default:
            main_dat.cfg_active_timeout = 0;
            usr_uart_send_a_msg(SET_ACTIVE_INFOR,"off");
            break;
    }

}

#if LCD_PANEL_ENABLE 
void update_in_out_information(int vi_adc, int vo_adc, int curr_adc){
    char str_buf[64]={0};
    int vo,vi,io;

    vi = ADC_CAL_VOL(vi_adc);
    // sprintf(str_buf,"%d.%dV",vi/1000u,vi%1000u);
    usr_get_dis_7_num_str(str_buf,"V",vi);
    // DDL_Printf("vi:%s\r\n", str_buf);
    lcd_ascii_str_dis(0,24u,ascii_16x16_font,str_buf); //input voltage

    vo = ADC_CAL_VOL(vo_adc);
    // sprintf(str_buf,"%d.%dV",vo/1000u,vo%1000u);
    usr_get_dis_7_num_str(str_buf,"V",vo);
    // DDL_Printf("vo:%s\r\n", str_buf);
    lcd_ascii_str_dis(4,64u,ascii_16x16_font_bold,str_buf); //out voltage

    io = ADC_CAL_O_CURR(curr_adc);
    // sprintf(str_buf,"%d.%dA",io/1000u,io%1000u);
    usr_get_dis_7_num_str(str_buf,"A",io);
    // DDL_Printf("io:%s\r\n", str_buf);
    lcd_ascii_str_dis(6,64u,ascii_16x16_font_bold,str_buf); //out current
    
}
#else
RAM_FUNC void update_in_out_information(int vi_adc, int vo_adc, int curr_adc){
    char vi_str[32]={0};
    char vo_str[32]={0};
    char io_str[32]={0};
    char po_str[32]={0};
    char msg_pkg[APP_CMD_FRAME_LEN_MAX]={0};

    int vo,vi,io;

    vi = ADC_CAL_VOL(vi_adc);
    usr_get_dis_7_num_str(vi_str,"V",vi);
    vo = ADC_CAL_VOL(vo_adc);
    usr_get_dis_7_num_str(vo_str,"V",vo);
    io = ADC_CAL_O_CURR(curr_adc);
    usr_get_dis_7_num_str(io_str,"A",io);

    usr_get_dis_7_num_str(po_str,"W",((io*vo)/1000u));

    sprintf(msg_pkg,"<%s=%s><%s=%s><%s=%s><%s=%s>",\
        INPUT_VOLTAGE_CMD,vi_str,\
        OUTPUT_VOLTAGE_CMD,vo_str,\
        OUTPUT_CURRENT_CMD,io_str,\
        OUTPUT_POWER_CMD,po_str);

    usr_uart_send(msg_pkg,strlen(msg_pkg));
}
#endif
